#!/bin/bash


: << 'COMMENT'
编写时间: 2025-03-17
编写人: 王杰民
修改人: 吴建波
编写功能: 该脚本用于tims-h5前端代码部署
         1 tims-h5压缩包解压缩，部署到对应路径
         2 将压缩包进行删除，并备份
         3 提示设置tims前端版本号
COMMENT




# 变量数据配置
db_ip="192.168.36.233"
db_port="3306"
db_name="tecrun_ca_client_bipfs"
db_username="root"
db_password="Tchzt123!"
db_type="mysql"
#db_type="gaosi"
#db_type="dameng"
#db_type="oracle"


linux_path="/home/tecrun/jenkins_publish9/ty_jar_auth"
tar_fie_url="http://192.168.53.144/E:/sh/ty_java_auth/tims-auth-sys-client.jar"
start_sh_fie_url="http://192.168.53.144/E:/sh/ty_java_auth/start.sh"
tar_name="tims-auth-sys-client.jar"
start_sh_name="start.sh"
config_path_tmp1="BOOT-INF/classes"
config_path_tmp2="/home/tecrun/jenkins_publish/tc_mgt_sys/BOOT-INF/classes"
config_name="application.yml"
service_name="tims-auth"

#jar xf tecrun-admin.jar /tims-auth-sys-client/BOOT-INF/classes/application.properties

# 判断文件夹是否存在，文件夹创建
if [ ! -d "$linux_path" ]; then
  echo "目录 $linux_path 不存在，请确认路径。"
  mkdir -p "$linux_path"
fi
cd "$linux_path" || exit

# 判断压缩包是否存在，存在时不进行压缩包下载
if [ ! -f "$tar_name" ]; then
  echo "未找到 $ttar_name 文件，开始下载..."
  wget "$tar_fie_url"
  wget "$start_sh_fie_url"
  if [ $? -eq 0 ]; then
    echo "下载成功！"
  else
    echo "下载失败！"
    exit 1
  fi
else
  echo "已找到 $tar_name 文件，跳过下载。"
  echo "已找到 $start_sh_name 文件，跳过下载。"
fi

# 判断jar文件存在进行初始配置文件提取
if [ -f "$tar_name" ]; then
  echo "已找到 $tar_name jar文件，开始提取..."
  jar xf $tar_name $config_path_tmp1"/application.yml"
  #jar xf $tar_name BOOT-INF/classes/application.yaml
  if [ $? -eq 0 ]; then
    echo "提取成功！"
  else
    echo "提取失败！"
    exit 1
  fi
fi

#将application.yml文件移动到启动目录下
if [ -f "$config_path_tmp1"/"$config_name" ]; then
  echo "已找到 $config_name 文件，开始移动..."
  mv "$config_path_tmp1"/"$config_name" "$linux_path"
  if [ $? -eq 0 ]; then
    echo "移动成功！"
  else
    echo "移动失败！"
    exit 1
  fi
else
  echo "未找到 $config_name 文件，跳过移动。"
fi

# 如果是mysql数据库，进行替换
if [ "$db_type" = "mysql" ]; then
  echo "开始替换mysql数据库配置..."
  # 构建新的数据库连接 URL
  new_url="jdbc:mysql://$db_ip:$db_port/$db_name?useSSL=false&serverTimezone=CTT&allowPublicKeyRetrieval=true"
  #url: jdbc:mysql://192.168.36.233:3306/tecrun_ca_client_bipfs?useSSL=false&serverTimezone=CTT&allowPublicKeyRetrieval=true
  # 使用 sed 命令替换数据库连接 URL 和密码
  echo $new_url
  sed -i "s|url:.*|url: |" "$linux_path"/"$config_name"
  sed -i "s|url:.*|url: $new_url|" "$linux_path"/"$config_name"
  sed -i "s|password:.*|password: $db_password|" "$linux_path"/"$config_name"
  sed -i "s|username:.*|username: $db_username|" "$linux_path"/"$config_name"
  if [ $? -eq 0 ]; then
    echo "替换成功！"
  else
    echo "替换失败！"
    exit 1
  fi
fi

mkdir logs






echo "部署完成。===================================================="
echo "部署路径：'$(pwd)'"











